package com.qzdsoft.erpcloud.service.stock.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierStatement;
import com.qzdsoft.erpcloud.domain.stock.StockOutbound;
import com.qzdsoft.erpcloud.mapper.stock.StockOutboundMapper;
import com.qzdsoft.erpcloud.mapper.stock.StockOutboundQueryMapper;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundService;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.stock.StockOutType;
import com.qzdsoft.vo.stock.StockOutboundStatus;


@Service
public class StockOutBoundServiceImpl implements StockOutBoundService
{   
    
    @Autowired
    private StockOutboundQueryMapper tStockOutboundQueryMapper;
    @Autowired
    private StockOutboundMapper stockOutboundMapper;
    @Autowired
    private CustomerSupplierStatementService customerSupplierStatementService;
    
    private Date data;
    
    @Override
    public LayTableResponse<StockOutboundInfo> findPurchaseOrdersBy(
            StockOutboundQueryInfo info)
    {
        PageHelper.startPage(info.getPage(), info.getLimit());
        List<StockOutboundInfo> purchaseOrders = tStockOutboundQueryMapper.selectAll(info);
        LayTableResponse<StockOutboundInfo> po = new LayTableResponse<StockOutboundInfo>(purchaseOrders);
        return po;
    }
    @Override
    public StockOutboundInfo findOrdersBy(Integer id)
    {
        return tStockOutboundQueryMapper.selectOne(id);
    }
    
    @Override
    @Transactional
    public Response<StockOutboundInfo> edit(StockOutboundEditInfo info)
    {   data=new Date();
        StockOutbound record = new StockOutbound();
        record.setId(info.getId());
        StockOutbound obInfo = stockOutboundMapper.selectOne(record);
        obInfo.setCustomerId(info.getSupplierId());
        obInfo.setCustomerContactId(info.getContacts());
        obInfo.setRemark(info.getRemark());
        
        obInfo.setStatementId(info.getStatementId());
        obInfo.setStatementNamevar(info.getStatementNamevar());
        obInfo.setStatementCurrencyId(info.getStatementCurrencyId());
        obInfo.setStatementDays(info.getStatementDays());
        obInfo.setStatementExchangeRate(info.getStatementExchangeRate());
        obInfo.setStatementTaxRate(info.getStatementTaxRate());
       
        
        obInfo.setModifyId(1);
        obInfo.setModifyTime(data);
        
        
        stockOutboundMapper.updateByPrimaryKeySelective(obInfo);
        
        StockOutboundInfo soInfo = tStockOutboundQueryMapper.selectOne(info.getId());
        return new Response<StockOutboundInfo>(200,"成功",soInfo);
    }
    
    
    @Override
    @Transactional
    public Response<String> save(StockOutbound info)
    {   data=new Date();
        info.setCreatorId(1);
        info.setCtime(data);
        info.setModifyId(1);
        info.setModifyTime(data);
        info.setReviewStatus(Integer.parseInt(StockOutboundStatus.NOT_REVIEW_CODE));
        stockOutboundMapper.insert(info);
        return new Response<String>(ResultEnum.SUCCESS);
    }
    
    @Override
    @Transactional
    public Response<String> delete(Integer id)
    {
        StockOutbound record = new StockOutbound();
        record.setId(id);
        stockOutboundMapper.delete(record);
        return new Response<String>(ResultEnum.SUCCESS);
    }
    
    
    @Override
    @Transactional
    public Response<String> review(Integer id, Integer status)
    {
        data =new Date();
        StockOutbound record = new StockOutbound();
        record.setId(id);
        record.setReviewStatus(status);
        record.setReviewId(1);
        record.setReviewTime(data);
        record.setModifyId(1);
        record.setModifyTime(data);
        stockOutboundMapper.updateByPrimaryKeySelective(record);
        return new Response<String>(ResultEnum.SUCCESS);
    }
    @Override
    public StockOutbound findOne(Integer id)
    {
        StockOutbound record = new StockOutbound();
        record.setId(id);
        return stockOutboundMapper.selectOne(record);
    }
	@Override
	public List<StockOutbound> selectReviewOrderByCompanyId(StockOutbound record) {
		
		return stockOutboundMapper.select(record);
	}
	/**
	 * 采购订单转采购退货单
	 * 
	 */
	@Transactional
    public Response<String> trasnlation(PurchaseOrderEditInfo info)
    {
	    StockOutbound stockOutbound = new StockOutbound();
        stockOutbound.setCompanyId(info.getCompanyid());
        stockOutbound.setSourceBillId(info.getId());
        stockOutbound.setSourceBillNo(info.getBillno());
        stockOutbound.setCustomerId(info.getSupplierId());
        stockOutbound.setCustomerContactId(info.getConnactId());
        stockOutbound.setOutboundType(StockOutType.PUOUT_CODE);
        stockOutbound.setStatementId(info.getSettleMethodId());
        stockOutbound.setStatementNamevar(info.getSettleMethodName());
        stockOutbound.setStatementDays(info.getSettleDays());
        stockOutbound.setStatementExchangeRate(Integer.parseInt(info.getExchargeRate()));
        stockOutbound.setStatementTaxRate(Integer.parseInt(info.getTaxRate()));
        CustomerSupplierStatement cs =customerSupplierStatementService.selectById(info.getSettleMethodId());
        stockOutbound.setStatementCurrencyId(cs.getCurrency());
        data=new Date();
        stockOutbound.setCreatorId(1);
        stockOutbound.setCtime(data);
        stockOutbound.setModifyId(1);
        stockOutbound.setModifyTime(data);
        stockOutbound.setReviewStatus(Integer.parseInt(StockOutboundStatus.NOT_REVIEW_CODE));
        stockOutboundMapper.insert(stockOutbound);
        
        //
        
        return new Response<String>(ResultEnum.SUCCESS);
    }
    
    

}
